package com.cartrentalsystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cartrentalsystem.domain.po.Employee;
import com.cartrentalsystem.domain.vo.Result;
import com.cartrentalsystem.service.IEmployeeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LHD
 * @since 2025-06-16
 */
@RestController
@RequestMapping("/employee")
@Slf4j
@RequiredArgsConstructor
public class EmployeeController {

    private final IEmployeeService employeeService;

    @GetMapping("/list")
    public Result<Page<Employee>> list(@Param("page") Integer page, @Param("size") Integer size, @Param("role") Integer role, @Param("name") String name,@Param("status") Integer status) {
        Result<Page<Employee>> result = employeeService.pageList(page, size, role, name, status);
        log.info("result:{}",result);
        return result;
    }

    @PostMapping("/add")
    public Result<String> add(@RequestBody Employee employee) {
        log.info("employee:{}",employee);
        return employeeService.addEmployee(employee);
    }

    @PutMapping("/update")
    public Result<String> update(@RequestBody Employee employee) {
        log.info("employee:{}",employee);
        return employeeService.updateEmployee(employee);
    }

    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Integer id) {
        log.info("id:{}",id);
        return employeeService.removeById(id) ? Result.success("删除成功") : Result.fail("删除失败");
    }

    @GetMapping("/names")
    public Result<String[]> getNames() {
        Result<String[]> success = Result.success(employeeService.list().stream()
                .filter(employee -> employee.getStatus() == 1)
                .filter(employee -> employee.getRole() == 2)
                .filter(employee -> employee.getDeleted() == 0)
                .map(Employee::getName).toList().toArray(String[]::new));
        log.info("result:{}",  success);
        return success;
    }

}
